package com.xinruke.hostels.dailymanage.job;

import com.xinruke.hostels.common.po.YwFangjian;
import com.xinruke.hostels.common.po.constants.FangtaiConstants;
import com.xinruke.hostels.dailymanage.interfaces.dto.roomstatuschange.RoomStatusChangeRecordAddDTO;
import com.xinruke.hostels.dailymanage.mapper.YwFangjianMapper;
import com.xinruke.hostels.dailymanage.mapper.YwRuzhuDingdanZixiangMapper;
import com.xinruke.hostels.dailymanage.service.RoomStatusChangeRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author yutaipu
 */
@Component
public class AutoUpdateRoomStatusJob {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private YwFangjianMapper ywFangjianMapper;

    @Autowired
    private RoomStatusChangeRecordService roomStatusChangeRecordService;


    @Value("${job.checkInUpdateRoomStatus.open}")
    private String checkInJobOpen;

    @Value("${job.checkOutUpdateRoomStatus.open}")
    private String checkOutJobOpen;

    /**
     * 住净的房间，2小时后，自动变为住脏
     */
    @Scheduled(cron = "${job.checkInUpdateRoomStatus.cron}")
    public void checkInUpdateRoomStatus() {
        // 入住超过两个小时的住净房间列表
        List<YwFangjian> roomList = ywFangjianMapper.listAllCheckIn2HourRoom();
        for (YwFangjian room : roomList) {
            if (!FangtaiConstants.FANGJIANZHUANGTAI_ZHUJING.equals(room.getZhuangtai())) {
                continue;
            }
            // 修改房间状态
            ywFangjianMapper.updateStatusById(room.getId(), FangtaiConstants.FANGJIANZHUANGTAI_ZHUZANG);

            // 记录房态变更日志
            RoomStatusChangeRecordAddDTO addDTO = new RoomStatusChangeRecordAddDTO();
            addDTO.setFangjianId(room.getId());
            addDTO.setFangjianbianhao(room.getFangjianbianhao());
            addDTO.setYuanzhuangtai(room.getZhuangtai());
            addDTO.setDangqianzhuangtai(FangtaiConstants.FANGJIANZHUANGTAI_ZHUZANG);
            addDTO.setBeizhu("入住两小时自动更新状态住净为住脏");
            addDTO.setKaishishijian(new Date());
            addDTO.setJieshushijian(new Date());
            addDTO.setYuanid(null);
            addDTO.setYewuleixing((byte) 1);
            roomStatusChangeRecordService.add(addDTO);
            logger.info("定时器：住净变住脏修改完成，fangjianId={} fangjianbianhao={}", room.getId(), room.getFangjianbianhao());
        }
    }

    /**
     * 办理退房后的房间，房间状态是空脏，1小时后自动变为空净
     */
    @Scheduled(cron = "${job.checkOutUpdateRoomStatus.cron}")
    public void checkOutUpdateRoomStatus() {
        List<YwFangjian> roomList = ywFangjianMapper.listAllCheckOut1HourRoom();
        for (YwFangjian room : roomList) {
            if (!FangtaiConstants.FANGJIANZHUANGTAI_KONGZANG.equals(room.getZhuangtai())) {
                continue;
            }
            // 修改房间状态
            ywFangjianMapper.updateStatusById(room.getId(), FangtaiConstants.FANGJIANZHUANGTAI_KONGJING);

            // 记录房态变更日志
            RoomStatusChangeRecordAddDTO addDTO = new RoomStatusChangeRecordAddDTO();
            addDTO.setFangjianId(room.getId());
            addDTO.setFangjianbianhao(room.getFangjianbianhao());
            addDTO.setYuanzhuangtai(room.getZhuangtai());
            addDTO.setDangqianzhuangtai(FangtaiConstants.FANGJIANZHUANGTAI_KONGJING);
            addDTO.setBeizhu("退房一小时自动更新状态住脏为空净");
            addDTO.setKaishishijian(new Date());
            addDTO.setJieshushijian(new Date());
            addDTO.setYuanid(null);
            addDTO.setYewuleixing((byte) 1);
            roomStatusChangeRecordService.add(addDTO);
            logger.info("定时器：住脏变空净修改完成，fangjianId={} fangjianbianhao={}", room.getId(), room.getFangjianbianhao());
        }
    }


}
